//
//  ACECourseViewController.m
//  ACE
//
//  Created by zhangtao on 2018/8/7.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACECourseViewController.h"
#import "ACECourseTableViewCell.h"
#import "JCBaseNavSearchView.h"
#import "AppDelegate.h"

@interface ACECourseViewController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSString *changeBtnImage;
@end

@implementation ACECourseViewController

{
    UITableView *table;
    SDCycleScrollView *cycleView;
    JCBaseNavSearchView *searchView;
}

-(SDCycleScrollView *)cycleView{
    if (!cycleView) {
        cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, onlyNavHeight, ACEWidth, 120) delegate:self placeholderImage:[UIImage imageNamed:@"placeholdImg1.png"]];
        cycleView.localizationImageNamesGroup = @[@"placeholdImg1.png",@"placeholdImg1.png",@"placeholdImg1.png",@"placeholdImg1.png",@"placeholdImg1.png",@"placeholdImg1.png"];
    }
    return cycleView;
}

-(JCBaseNavSearchView *)searchView{
    if (!searchView) {
        searchView = [[JCBaseNavSearchView alloc]initWithFrame:CGRectMake(15,statusBarHeight+10, ACEWidth-60, 30)];
        searchView.searchBarIcon = @"学生搜索icon";
        searchView.boardColor = lightGreen;
        
    }
    return searchView;
}

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _changeBtnImage = @"转换";
    if (@available(iOS 11.0,*)) {
        table.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
   
    [self createNavView];
    [self createTable];
//    self.navigationItem.titleView = self.searchView;
//    [self initNavRightWithImage:@"转换"];
}

-(void)changeRole:(UIButton *)btn{
   
    [btn setImage:[UIImage imageNamed:_changeBtnImage] forState:UIControlStateNormal];

    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [app changeRole];
}

-(void)createTable{
    if (!table) {
        table = [[UITableView alloc]initWithFrame:CGRectMake(0, navHeight, ACEWidth, ACEHeight-navHeight-tabBarHeight) style:UITableViewStyleGrouped];
        table.delegate = self;
        table.dataSource = self;
        [self.view addSubview:table];
        table.separatorStyle = UITableViewCellSeparatorStyleNone;
        [table registerNib:[UINib nibWithNibName:@"ACECourseTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        table.sectionHeaderHeight = 50;
    }
}

-(void)createNavView{
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (app.baseTabBarController.role == appRole_teacher) {
        _changeBtnImage = @"学生转换";
    }else{
        _changeBtnImage = @"转换";
    }
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ACEWidth, navHeight)];
    [view addSubview:self.searchView];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectZero];
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(searchView);
        make.right.mas_equalTo(view).mas_offset(-10);
        make.width.mas_equalTo(@30);
        make.height.mas_equalTo(@30);
    }];
    
  
    [button setImage:[UIImage imageNamed:_changeBtnImage] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(changeRole:) forControlEvents:UIControlEventTouchUpInside];
//    [view addSubview:button];
    [self.view addSubview:view];
    [self.view bringSubviewToFront:view];
    
//    self.tableView.tableHeaderView = view;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //715/293
    return ACEWidth*293/715;// /(715/293);//   130;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 120+onlyNavHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return .1;
}

//-(UIView *)

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.cycleView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ACECourseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    return cell;
}


-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
